import { Approval } from 'approval-model.js';
var api = new Approval();
var Function = require("../../../../utils/function.js");
var app = getApp();
Page({
  data: {
    //tab
    Tab:['待我审批','我已审批'],
    Tdex:0,
    //页数
    page:1,
    //列表
    list:[],
    //节点、
    he:false,
  },

  //tab切换
  bindtab:function(e){
    this.setData({ Tdex: e.target.dataset.index })
    this.setData({ page:1 })
    this.getInitial();
  },

  //路由跳转
  linkTo(e) {
    let state = Function.getDataSet(e, 'state');
    let id = Function.getDataSet(e, 'id');
    console.log(state)
    switch (state){
      case 1:
        Function.linkTo('/pages/work/MyApproval/vacation/vacation?id='+id) 
        break;

      case 2:
        Function.linkTo('/pages/work/MyApproval/travel/travel?id='+id)
        break;   

      case 3:
        Function.linkTo('/pages/work/MyApproval/repair/repair?id='+id)
        break;              
    }
  },

  //获取列表
  bindList(e){
    let data = { page: e }
    if (this.data.Tdex == 0){
      api.getList(data, (res) => {
        if (res.code == 0) {
          app.showError(res.msg, function () {
            wx.navigateBack();
          });
        }
        let array = this.data.list;
        let list = array.concat(res.data);
        this.setData({ list: list });
      })
    }else{
      api.getList2(data, (res) => {
        if (res.code == 0) {
          app.showError(res.msg, function () {
            wx.navigateBack();
          });
        }
        let array = this.data.list;
        let list = array.concat(res.data);
        this.setData({ list: list });
      })
    }
  },

  //初始列表
  getInitial(){
    let date = ''
    api.getSign({ date: date, is_log: 1 }, (res) => {
      if (res.code == 0) {
        app.showError(res.msg, function () {
          wx.navigateBack();
        });
      }else{
        if (this.data.Tdex == 0) {
          let data = { page: 1 }
          api.getList(data, (res) => {
            this.setData({ list: res.data });
          })
        } else {
          let data = { page: 1 }
          api.getList2(data, (res) => {
            this.setData({ list: res.data });
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.setData({
      isLogin: app.checkIsLogin()
    });
    if (_this.data.isLogin === false) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.setData({ Tdex: options.index });
    this.bindDom();
  },

  //列表触底
  bindBottom(e) {
    let pageNo = this.data.page + 1;
    this.setData({ page: pageNo })
    this.bindList(pageNo);
  },

  //获取节点
  bindDom() {
    wx.createSelectorQuery().select('.flex-gl').boundingClientRect(res => {
      this.setData({ he: res.height })
    }).exec();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ page:1 })
    this.getInitial();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})